
//calcule la taille de la chaine
int str_len(char* string){
    //i = 0
    int i = 0;

    //tant que le charactere \0 (fin de chaine) n'as pas ete rencontre, i = i+1
    while(string[i++]!='\0');

    //on retourne i
    return i;
}

char* my_concat(char* string1, char* string2){
	int size1 = str_len(string1);
	int size2 = str_len(string2);
	
	char* newString = (char*)malloc(sizeof(char)*(size1+size2));
	int i = 0;
	while(i<size1){
		newString[i] = string1[i];
		i++;
	}
	i = 0;
	while(i<size2){
		newString[i+size1] = string2[i];
		i++;
	}
	newString[size1+size2]= '\0';
	return newString;
}

//retourne la charactere present au milieu de la chaine passee en parametre
char getMiddleChar(char* string){
    //recuperation de la taille de la chaine passee en parametre
    int length = str_len(string);
    //on retourne le caracetre present a l'index taille/2
    return string[(length/2)-1];
}

//copy a string into another one and returns it
char* str_copy(char* str){
    //get size of the string to copy
    int size = str_len(str);
    //allocating memory for the second string
    char* string2 = (char*)malloc(sizeof(char)*size);
    int i = 0;
    //copy char one by one from str to string2
    while(i<size){
        string2[i] = str[i++];
    }

    //returns string2
    return string2;
}

//copy the content of the file located at filename in a char** array and returns it
//in the returned array, one index = one line, ie:
//returnedTab[0] = "Line1"
//returnedTab[1] = "Line2"
char** getFileContent(char* filename, int *nblines){
    //open the file
    FILE *file = fopen ( filename, "r" );
    //prepare tab in memory
    char** tab = (char**)malloc(2048);
    int i = 0;
    //test if file is present
    if ( file != NULL )
    {
        //create a temporary line
        char line[512]; /* or other suitable maximum line size */
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
            //put the line in the returned array
            tab[i] = str_copy(line);
            //augment the returned array index for the next line
            i++;
        }
        //close the file
        fclose ( file );
    }
    else
    {
        //print error if file cant be opened
        perror ( filename ); /* why didn't the file open? */
    }
	*nblines = i;
    //return the built in array
    return tab;
}

void printFileContent(char *filename){
    //open the file
    FILE *file = fopen ( filename, "r" );

    int i = 0;
    //test if file is present
    if ( file != NULL )
    {
        //create a temporary line
        char line[512]; /* or other suitable maximum line size */
        while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */
        {
			printf("%s", line);
            //augment the returned array index for the next line
            i++;
        }
        //close the file
        fclose ( file );
    }
    else
    {
        //print error if file cant be opened
        perror ( filename ); /* why didn't the file open? */
    }
    //return the built in array
}

int in_array(int array[], int count, int value){
  
  int i = 0;
  while(i<count){
    if(array[i]==value){
	return 1;
    }
    i++;
  }
  return 0;
}

char my_itoa(int num)
{
	char num_c = num+48;
	return num_c;
}

int my_strcmp(char* arg1, char* arg2)
{
	int i;
	int taille1 = str_len(arg1);
	int taille2 = str_len(arg2);

	if (taille1>taille2)
	{
		return 1;
	}
	else if(taille1<taille2)
	{
		return -1;
	}
	else
	{
		for (i=0;i<taille1;i++)
		{
			if(arg1[i]>arg2[i])
			{
				return 1;
			}
			if(arg1[i]<arg2[i])
			{
				return -1;
			}
		}
		return 0;
	}
}
